//
//  QHKeyboardNumPad.m
//  QHKeyboard
//
//  Created by 王庆华 on 16/11/24.
//  Copyright © 2016年 王庆华. All rights reserved.
//

#import "QHKeyboardNumPad.h"

#import "NSDate+VR.h"


@interface QHKeyboardNumPad () <QHKeyboardBtnDelegate>

@property (nonatomic, strong) NSMutableArray *btnArray;
@property (nonatomic, weak) UITextField *responder;

@end

@implementation QHKeyboardNumPad

- (UITextField *)responder{
    //    if (!_responder) {  // 防止多个输入框采用同一个inputview
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView *firstResponder = [keyWindow valueForKey:@"firstResponder"];
    _responder = (UITextField *)firstResponder;
    //    }
    return _responder;
}

- (instancetype)initWithFrame:(CGRect)frame{
   
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self addControl];
        
        self.date = [NSDate date];
    }
    return self;
}

- (void)addControl {
   
    self.btnArray = [NSMutableArray array];
    
    NSArray *array = @[@"7", @"8", @"9", @"今天", @"4", @"5", @"6", @"+", @"1", @"2", @"3", @"-", @".", @"0", @"", @"完成"];
    for (int i = 0; i < 16; i++) {
        
        QHKeyboardBtn *btn1 = [QHKeyboardBtn buttonWithTitle:array[i] tag:i delegate:self];
        [self addSubview:btn1];
        [self.btnArray addObject:btn1];
        
        if (i == 14) {
            [btn1 setImage:[UIImage imageNamed:@"tally_keyboard_del"] forState:UIControlStateNormal];
        }
        if (i == 15) {
            btn1.backgroundColor = MAINColor;
        }
    }
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    CGFloat btnW = (self.frame.size.width - 3*margin)/4;
    CGFloat btnH = (self.frame.size.height - 4*margin)/4;
    
    for (QHKeyboardBtn *btn in self.btnArray) {
        
        CGFloat btnX = btn.tag % 4 * (btnW + margin);
        CGFloat btnY = margin + btn.tag / 4 * (btnH + margin);
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
}


- (void)deleteBtnClick {
    
    if (self.responder.text.length) {
        self.responder.text = [self.responder.text substringToIndex:self.responder.text.length-1];
    }
}

- (void)okBtnClick{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

#pragma mark - QHKeyboardBtnDelegate
-(void)KeyboardBtnDidClick:(QHKeyboardBtn *)btn{
    
    if (btn.tag == 14) {
        
        [self deleteBtnClick];
        if ([self.delegate respondsToSelector:@selector(KeyboardNumPadDidClickedWith:buttonType:)]) {
            [self.delegate KeyboardNumPadDidClickedWith:nil buttonType:KeyBoardButtonTypeDelete];
        }
    }else if (btn.tag == 15){
        
        [self okBtnClick];
        if ([self.delegate respondsToSelector:@selector(KeyboardNumPadDidClickedWith:buttonType:)]) {
            [self.delegate KeyboardNumPadDidClickedWith:nil buttonType:KeyBoardButtonTypeOK];
        }
    }else if (btn.tag == 3){
        
        if ([self.delegate respondsToSelector:@selector(KeyboardNumPadDidClickedWith:buttonType:)]) {
            [self.delegate KeyboardNumPadDidClickedWith:btn.titleLabel.text buttonType:KeyBoardButtonTypeDate];
        }
    }else if (btn.tag == 12){
        
        [self okBtnClick];
        if ([self.delegate respondsToSelector:@selector(KeyboardNumPadDidClickedWith:buttonType:)]) {
            [self.delegate KeyboardNumPadDidClickedWith:btn.titleLabel.text buttonType:KeyBoardButtonTypePoint];
        }
    }else{
        
        self.responder.text = [self.responder.text stringByAppendingString:btn.titleLabel.text];
        if ([self.delegate respondsToSelector:@selector(KeyboardNumPadDidClickedWith:buttonType:)]) {
            [self.delegate KeyboardNumPadDidClickedWith:btn.titleLabel.text buttonType:KeyBoardButtonTypeNumber];
        }
    }
}
/**
 *  用颜色返回一张图片
 */
- (UIImage *)createImageWithColor:(UIColor *)color {
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

#pragma mark - setter && getter
- (void)setDate:(NSDate *)date
{
    _date = date;
    
    QHKeyboardBtn *dateBtn = self.btnArray[3];
    if ([date isToday]) {
        
        [dateBtn setTitle:@"今天" forState:UIControlStateNormal];
        dateBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Light" size:20];
    }else{
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy/MM/dd";
        
        [dateBtn setTitle:[formatter stringFromDate:date] forState:UIControlStateNormal];
        dateBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Light" size:15];
    }
    
}

@end

